package com.demo.math.equationImpl;

import cn.hutool.core.util.RandomUtil;
import com.demo.math.domain.MathStrategy;

public class AddSub3MathStrategy implements MathStrategy {
    @Override
    public String math(int range) {
        int h1 = RandomUtil.randomBoolean() ? 1 : -1;
        int h2 = RandomUtil.randomBoolean() ? 1 : -1;
        return equation(h1, h2) + "=";
    }

    private String equation(int h1, int h2) {
        int a = RandomUtil.randomInt(1,10);
        int b = RandomUtil.randomInt(10);
        int c = RandomUtil.randomInt(10);

        int temp = a;
        if (h1 > 0) {
            temp += b;
        } else {
            temp -= b;
        }

        if (h2 > 0) {
            temp += c;
        } else {
            temp -= c;
        }

        if (temp < 0) {
            return equation(h1, h2);
        }

        return a +  (h1 > 0 ? "+" : "-") + "" + b + (h2 > 0 ? "+" : "-") + c;
    }
}
